package com.hdoit.ares.logical.acc.dao.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import com.alibaba.druid.util.StringUtils;
import com.hdoit.ares.logical.acc.dao.IIncomeDetailDao;
import com.hdoit.ares.logical.acc.model.IncomeDetail;
import com.hdoit.ares.logical.viewmodel.acc.FindIncomeDetail;
import com.hdoit.ares.logical.viewmodel.acc.IncomeDetailSearch;
import com.hdoit.ares.selfcommon.enums.acc.HesitatePeriodState;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      lvsi
 * @email       
 * @createtime  2017年12月19日 11:09:45
 * @comment
 */
@Component
public class IncomeDetailDaoImpl extends SuperDao<IncomeDetail> implements IIncomeDetailDao{

	@Override
	public List<IncomeDetail> queryIncomeDetail(IncomeDetailSearch search,String custId) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(IncomeDetail.class)
						.andEq("customerId",custId);
	    return queryList(finderExpression);
	}

	@Override
	public List<IncomeDetail> queryIncomeDetailInfo(String custId) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(IncomeDetail.class)
						.andEq("customerId",custId);
	    return queryList(finderExpression);
	}

	@Override
	public IncomeDetail querySumIncomeMoney(FindIncomeDetail findIncomeDetail,String custId) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("sum(incomeAccount) as incomeMoney")
		                .from(IncomeDetail.class)
		                .andEq("customerId", custId)
		                .andEq("state","income");
		
		appendFinder(findIncomeDetail,finderExpression);
		
		return queryModel(finderExpression);
	}

	@Override
	public IncomeDetail querySumExpendMoney(FindIncomeDetail findIncomeDetail,String custId) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("sum(incomeAccount) as expendMoney")
		                .from(IncomeDetail.class)
		                .andEq("customerId", custId)
		                .andEq("state","pay");
		
		appendFinder(findIncomeDetail,finderExpression);
		
		return queryModel(finderExpression);
	}

	@Override
	public Boolean updateHesitatePeriodState(Date lastDate) {
        IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		
		finderExpression.update(IncomeDetail.class)
						.set("hesitatePeriodState=#{hesitatePeriodState}")
						.andWhere("TIMESTAMPDIFF(DAY,time,#{lastDate})>=hesitatePeriod")
						.andWhere("hesitatePeriodState=#{oldState}")
						.setParam("oldState", HesitatePeriodState.Hesitating)
						.setParam("hesitatePeriodState",HesitatePeriodState.Hesitated)
						.setParam("lastDate", lastDate);
		
		return update(finderExpression) > 0;
	}
	
	@Override
	public IncomeDetail queryIncomeInHesitate(String id) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("SUM(`incomeAccount`) as sumincome")
		                .from(IncomeDetail.class)
		                .andEq("customerId", id)
		                .andEq("state","income")
		                .andEq("hesitatePeriodState","hesitating");
		
		return queryModel(finderExpression);
		
	}

	@Override
	public List<IncomeDetail> queryIncomeDetail(FindIncomeDetail findIncomeDetail, String custId) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(IncomeDetail.class)
						.andEq("customerId", custId);
		
		appendFinder(findIncomeDetail,finderExpression);

		return queryList(finderExpression);
	}
	
	private void appendFinder(FindIncomeDetail findIncomeDetail,IFinderExpression finderExpression){
	if(!StringUtils.isEmpty(findIncomeDetail.getIncomeState())){
		finderExpression.andEq("incomeState", findIncomeDetail.getIncomeState());
	}
	
	if(!StringUtils.isEmpty(findIncomeDetail.getOrder())){
		if("desc".equals(findIncomeDetail.getUpOrDown())){
			finderExpression.addOrder(findIncomeDetail.getOrder(), OrderEnum.desc);
		}
		if("asc".equals(findIncomeDetail.getUpOrDown())){
			finderExpression.addOrder(findIncomeDetail.getOrder(), OrderEnum.asc);
		}
	}
	if(findIncomeDetail.getStartDate()!=null){
		finderExpression.andWhere("TIMESTAMPDIFF(SECOND,#{startDate},time)>=0")
		  .setParam("startDate", findIncomeDetail.getStartDate());
	}
	if(findIncomeDetail.getEndDate()!=null){
		finderExpression.andWhere("TIMESTAMPDIFF(SECOND,#{endDate},time)<=0")
		  .setParam("endDate", findIncomeDetail.getEndDate());
	}
}

	@Override
	public IncomeDetail queryIncomeDetail(String id) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("*")
		                .from(IncomeDetail.class)
		                .andEq("id", id);
		
		return queryModel(finderExpression);
	}

	//分页查询奖金详情列表 
	@Override
	public List<IncomeDetail> queryIncomeList(FindIncomeDetail findIncomeDetail, String custId, Integer start,Integer length) {
		
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(IncomeDetail.class)
						.andEq("customerId", custId)
						.limit(start, length);
		
		appendFinder(findIncomeDetail,finderExpression);

		return queryList(finderExpression);
	}

	/*//分页查询奖金总收入
	@Override
	public IncomeDetail querySumIncome(FindIncomeDetail findIncomeDetail,
			String custId, Integer start, Integer length) {
		
		IFinderExpression oneChildIdFinder=FinderExpressionFactory.createExpression();
		oneChildIdFinder.select("*")
						.from(IncomeDetail.class)
						.andWhere("customerId=#{custId}")
						.limit(start, length);
		
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("sum(incomeAccount) as incomeMoney")
		                .from("("+oneChildIdFinder.toSql()+") as nowTable")
		                .andEq("state","income")
		                .setParam("custId", custId);
		
		appendFinder(findIncomeDetail,finderExpression);
		
		return queryModel(finderExpression);
	}

	//分页查询奖金总支出
	@Override
	public IncomeDetail querySumExpend(FindIncomeDetail findIncomeDetail,
			String custId, Integer start, Integer length) {
		
		IFinderExpression oneChildIdFinder=FinderExpressionFactory.createExpression();
		oneChildIdFinder.select("*")
						.from(IncomeDetail.class)
						.andWhere("customerId=#{custId}")
						.limit(start, length);
		
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("sum(incomeAccount) as expendMoney")
		                .from("("+oneChildIdFinder.toSql()+") as nowTable")
		                .andEq("state","pay")
		                .setParam("custId", custId);
		
		appendFinder(findIncomeDetail,finderExpression);
		
		return queryModel(finderExpression);
	}
*/
}
